/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.TrainingSubject;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-9-24上午10:52:19
 */
public class TrainingSubjectCreateActionFilter extends BaseRequestFilter implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		String code = actionRequest.getParameter("code");
		String summary = actionRequest.getParameter("summary");
		String period = actionRequest.getParameter("period");
		String startTime = actionRequest.getParameter("startTime");

		String endTime = actionRequest.getParameter("endTime");
		String description = actionRequest.getParameter("description");
		String price = actionRequest.getParameter("price");
		String type = actionRequest.getParameter("type");
		
		if((code!=null)&&(summary!=null)&&(period!=null)&&(startTime!=null)&&
				(endTime!=null)&&(description!=null)&&(price!=null)&&(type!=null)){

			float periodFloat = DataTypeUtil.parseFloat(period);
			int priceInt = DataTypeUtil.parseInt(price);
			
			if((periodFloat>0)&&(priceInt>=0)){
				
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				TrainingSubject vTrainingSubject = new TrainingSubject();
				vTrainingSubject.setCode(code);
				vTrainingSubject.setSummary(summary);
				vTrainingSubject.setPeriod(periodFloat);				
				vTrainingSubject.setStartTime(DataTypeUtil.parseTimeAMPM(startTime));
				vTrainingSubject.setEndTime(DataTypeUtil.parseTimeAMPM(endTime));
				vTrainingSubject.setDescription(description);
				vTrainingSubject.setPrice(priceInt);
				vTrainingSubject.setType(type);
				em.persist(vTrainingSubject);
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();

				success = true;
			}
		}else{
			success = false;
		}

		if(success)return SUCCESS;
		else return FAILED;
	}

}
